<template>
	<view class="payview">
		<view class="contact_box">
			<text style="text-align: center;">账单</text>
			<view class="contact_box1">
				<image src="../../static/iocn34.png"></image>
				<input type="text" v-model="totalFee" placeholder="00.00" />
			</view>
			<view class="contact_box2">
				<text>收款方</text>
				<text>省运慧商户</text>
			</view>
			<button @click="CreateTrade()">立即支付</button>
		</view>
	</view>
</template>

<script>
	import {
		CreateTrade,
		MiniProgramPay,
	} from '@/public/Api/update.js'
	import {
		setValue,
		getValue
	} from '@/public/storeage/index.js'
	export default {
	data() {
	           return {
	           totalFee:'',
	           }
	       },
	       onLoad(options) {
             this.totalFee=options.totalFee
	               },
	               methods: {
					// 创建交易订单
					async CreateTrade() {
						var payerUserId = getValue('id')
						var amount = this.totalFee
						var payDestination = this.payDestination
						var canInvoiced = this.canInvoiced
						var payType = 2
						if (amount == '') {
							uni.showToast({
								title: "充值金额不能为空",
								icon: 'none',
								duration: 2000
							})
							return false
						}
						try {
							const {
								data: res
							} = await CreateTrade({
								payerUserId,
								amount,
								payDestination,
								canInvoiced,
								payType
							})
							this.MiniProgramPay(res.result.data.tradeNumber)
						} catch (e) {}
					},
					async MiniProgramPay(res) {
						var outTradeNo = res
						var body = 'string'
						var totalFee = JSON.parse(this.totalFee * 100)
						var spBillCreateIp = '127.0.0.1'
						var notifyUrl = 'https://pay.syh56.com:44352/api/WeChatPayNotify/Unifiedorder'
						var tradeType = 'JSAPI'
						var openId = getValue('wxOpenId')
						uni.request({
							url: "https://pay.syh56.com:44352/api/WeChatPay/MiniProgramPay",
							method: "post",
							header: {
								'Abp.TenantId': '1',
								'Content-Type': "application/json",
							},
							data: {
								outTradeNo,
								body,
								totalFee,
								spBillCreateIp,
								notifyUrl,
								tradeType,
								openId
							},
							success: (res) => {
							var parameter = JSON.parse(res.data.parameter)
								this.doWxPay(parameter)
							}
						})
					},
					
					
					doWxPay(parameter) {
						var param = parameter
						//小程序发起微信支付
						uni.requestPayment({
							nonceStr: param.nonceStr,
							timeStamp: param.timeStamp, //记住，这边的timeStamp一定要是字符串类型的，不然会报错
							// out_trade_no: param.out_trade_no,
							appId: param.appId,
							package: param.package,
							signType: param.signType,
							paySign: param.paySign,
							success: function(event) {
								uni.showToast({
									title: '支付成功',
									icon: 'success',
									duration: 2000
								});
								setTimeout(function() {
									uni.redirectTo({
										url: '/pages/Calllog/sjindex',
									})
								}, 2000)
							},
							fail: function(error) {
								// let id = 1
								uni.showToast({
									title: '支付失败',
									icon: 'error',
									duration: 2000
								});
							},
				   })
			 }
			 },
			 }
	
</script>

<style lang="scss">
	page {
	  background-color: rgba(247, 247, 247, 1);
	  padding: 0 15upx;
	}
	.contact_box {
		padding: 40upx;
	
		text {
			display: block;
			font-size: 35upx;
			font-weight: bold;
		}
	
		.contact_box1 {
			display: flex;
			align-items: center;
			padding-top: 80upx;
	        padding-left: 40%;
			image {
				width: 40upx;
				height: 40upx;
			}
	
			input {
				font-size: 38upx;
				font-weight: bold;
				padding-left: 10upx;
			}
		}
		.contact_box2{
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			width: 100%;
			height: 100upx;
			box-shadow: 0px 2px 4px  rgba(0, 0, 0, 0.25);
			margin-top: 50upx;
			text{
				padding: 10upx 20upx;
			}
		}
		button{
			background-color: rgba(239, 77, 61, 1);
			color: #fff;
			width: 100%;
			margin-top: 50upx;
		}
	}
</style>